// components/randomDoor/randomDoor.js
const app = getApp();
const db = wx.cloud.database();
const _ = db.command
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    userInfo: {
      type: Object,
      value: null
    },
    home:{
      type: String,
      value: ''
    },
    msg:{
    type: String,
    value: ''
  },
   
  },

  /**
   * 组件的初始数据
   */
  data: {
    showmodal: false,
    showPopover:true,
    petMenuShow: false,  
    emojiIndex:Math.floor(Math.random() *3),
    globalUser: app.globalData.userInfo,
    emojis: [{
      name: '微笑',
      src: 'emoji/wexiao.svg'
    },
    {
      name: '花心',
      src: "emoji/huaxin.svg"
    }, {
      name: '开心',
      src: "emoji/kaixin.svg"
    }, {
      name: '不开心',
      src: "emoji/bukaixin.svg"
    }, {
      name: '惊恐',
      src: "emoji/jingkong.svg"
    }
  ],
    petMenu: [{
        action: 'robGold',
        icon: 'actions/robGold.svg',
        text: '打劫金币',
        bg:'rgb(150, 225, 255)'
      },
      {
        action: 'robTreasure',
        icon: 'actions/robTreasure.svg',
        text: '打劫宝石',
        bg:'rgb(150, 225, 255)'
      
      },
      {
        action: 'hit',
        icon: 'actions/hit.svg',
        text: '殴打精灵',
        bg:'rgb(150, 225, 255)'
      },
      {
        action: 'feed',
        icon: 'actions/feed.svg',
        text: '喂食精灵'
      },
      {
        action: 'giveGold',
        icon: 'actions/giveGold.svg',
        text: '赠送金币'
      },
      {
        action: 'randomDoor',
        icon: 'actions/door.svg',
        text: '继续串门'
      },
      // {
      //   action:'msg',
      //   icon:'actions/msg.svg',
      //   text:'雁过留声'
      // },
    ],
    myPetMenu: [

      {
        action: 'feedMyPet',
        icon: 'actions/feed.svg',
        text: '喂食精灵'
      },
      {
        action: 'train',
        icon: 'actions/train.svg',
        text: '训练精灵'
      },
      // {
      //   action: 'change',
      //   icon: '../../images/ui/actions/change.svg',
      //   text: '更换精灵'
      // },
   
      {
        action: 'openBox',
        icon: 'actions/box.svg',
        text: '钻石盲盒'
      },
      {
        action: 'dial',
        icon: 'actions/dial.svg',
        text: '趣味抽奖'
      },
      {
        action: 'randomDoor',
        icon: 'actions/door.svg',
        text: '任意串门'
      },
      // {
      //   action:'msg',
      //   icon:'../../images/ui/actions/msg.svg',
      //   text:'给访客留言'
      // },
    ]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    toggleMenu() {
      this.triggerEvent('hideMenu','pet')
      this.setData({
        petMenuShow:this.data.petMenuShow?false: true,    
        emojiIndex:Math.floor(Math.random() *4) 
      })
    },
    hideMenu(){
      this.setData({
        petMenuShow:false
      })   
    },
hidePopover(){
  this.setData({
showPopover:false
})
},

//行为
    robGold() {
      this.setData({     
        emojiIndex:4 
      })
      wx.cloud.callFunction({
        name: 'pet',
        data: {
          op: 'robGold',
          _openid: this.data.userInfo._openid
        }
      }).then(res => {
        console.log(res)
        if (res.result == 'notEnough') {
          wx.showToast({
            title: '你的魅力值太低了，不能再打劫了',
            icon: 'none'
          })
          return false
        }

        wx.showToast({
          title: '抢得 ' + res.result.gold + ' 金币，你的魅力值 -' + res.result.charm,
          icon: 'none'
        })
        this.triggerEvent("getUserInfo");
      })
//      this.hideMenu()
    },
    robTreasure() {
      this.setData({     
        emojiIndex:4 
      })
      wx.cloud.callFunction({
        name: 'pet',
        data: {
          op: 'robTreasure',
          _openid: this.data.userInfo._openid
        }
      }).then(res => {
        console.log(res)
        if (res.result == 'notEnough') {
          wx.showToast({
            title: '你的魅力值太低了，不能再打劫了',
            icon: 'none'
          })
          return false
        }
        wx.showToast({
          title: '抢得 ' + res.result.treasureAmount + ' 宝石，你的魅力值 -' + res.result.charm,
          icon: 'none'
        })
        this.triggerEvent("getUserInfo");
      })
//      this.hideMenu()
    },
    hit() {
      this.setData({     
        emojiIndex:4 
      })
      wx.cloud.callFunction({
        name: 'pet',
        data: {
          op: 'hit',
          _openid: this.data.userInfo._openid
        }
      }).then(res => {
        console.log(res)
        if (res.result == 'notEnough') {
          wx.showToast({
            title: '你的魅力值太低了，不能再打别人精灵了',
            icon: 'none'
          })
          return false
        }
        wx.showToast({
          title: '殴打无辜，你的魅力值 -' + res.result.charm,
          icon: 'none'
        })
        this.triggerEvent("getUserInfo");
      })
//      this.hideMenu()
    },
    feed() {
      this.setData({     
        emojiIndex:Math.floor(Math.random() *3)
      })
      wx.cloud.callFunction({
        name: 'pet',
        data: {
          op: 'feed',
          _openid: this.data.userInfo._openid
        }
      }).then(res => {
        console.log(res)
        if (res.result == 'enough') {
          wx.showToast({
            title: 'TA的精灵生命值满满，暂时无需喂养，谢谢',
            icon: 'none'
          })
          return false
        }
        if (res.result == 'goldNotEnough') {
          wx.showToast({
            title: '你的金币不够了，还是谢谢你的爱心',
            icon: 'none'
          })
          return false
        }
        wx.showToast({
          title: res.result.charm == 0 ? '乐于助人，魅力满满' : '帮助他人，精灵魅力值 +' + res.result.charm,
          icon: 'none'
        })
        this.triggerEvent("getUserInfo");
      })
 //     this.hideMenu()
    },
    giveGold() {
      this.setData({     
        emojiIndex:Math.floor(Math.random() *3)
      })
      wx.cloud.callFunction({
        name: 'pet',
        data: {
          op: 'giveGold',
          gold: 100,
          _openid: this.data.userInfo._openid
        }
      }).then(res => {
        console.log(res)
        if (res.result == 'goldNotEnough') {
          wx.showToast({
            title: '金币不够了，无法赠送TA金币',
            icon: 'none'
          })
          return false
        }
        wx.showToast({
          title: res.result.charm == 0 ? '乐善好施，魅力满满' : '乐善好施，精灵魅力值 +' + res.result.charm,
          icon: 'none'
        })
        this.triggerEvent("getUserInfo");
      })
//      this.hideMenu()
    },
    msg() {

    },
    feedMyPet() {
      this.setData({     
        emojiIndex:Math.floor(Math.random() *3)
      })
      wx.cloud.callFunction({
        name: 'pet',
        data: {
          op: 'feedMyPet',
          //   _openid:this.data.userInfo._openid
        }
      }).then(res => {
        console.log(res)
        if (res.result == "enough") {
          wx.showToast({
            title: '精灵生命值满满，暂时无需喂养',
            icon: 'none'
          })
          return false
        }
        if (res.result == 'goldNotEnough') {
          wx.showToast({
            title: '金币不够了，无法喂养自己的精灵',
            icon: 'none'
          })
          return false
        }
        wx.showToast({
          title: '精灵生命值 +' + res.result.life,
          icon: 'none'
        })
        this.triggerEvent("getUserInfo");
      })
//      this.hideMenu()
    },
    train() {
      this.setData({     
        emojiIndex:Math.floor(Math.random() *3)
      })
      wx.cloud.callFunction({
        name: 'pet',
        data: {
          op: 'train',       
        }
      }).then(res => {
        console.log(res)
        if (res.result == "enough") {
          wx.showToast({
            title: '精灵魅力值满满，暂时无需训练',
            icon: 'none'
          })
          return false
        }
        if (res.result == 'goldNotEnough') {
          wx.showToast({
            title: '金币不够了，无法训练自己的精灵',
            icon: 'none'
          })
          return false
        }
        wx.showToast({
          title: '精灵魅力值 +' + res.result.charm,
          icon: 'none'
        })
        this.triggerEvent("getUserInfo");
      })
     // this.hideMenu()
    },
    change() {
      this.setData({
        showmodal: 'change'
      });
 
    },
    
    openBox() {
      wx.navigateTo({
        url: '/pages/openBox/openBox',
      })
     },
     dial() {
       wx.navigateTo({
         url: '/pages/dial/dial',
       })
     },
     randomDoor() {
       wx.redirectTo({
         url: '/pages/randomUserHome/randomUserHome',
       })
     },
  },
  ready:function(){
    this.setData({     
      emojiIndex:Math.floor(Math.random() *3),    
    })
  }
  
})